package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制层
 * @author Xiao
 * @date 2021/9/29
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 获取购物车数据
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> cartListApi() {
        List<ShoppingCart> shoppingCarts = shoppingCartService.cartListApi();
        return R.success(shoppingCarts);
    }
    /**
     * 添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addCartApi(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart = shoppingCartService.addCartApi(shoppingCart);
        return R.success(shoppingCart);
    }
    /**
     * 从购物车减去菜品/套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> updateCartApi(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        log.info("dishId:{}, setmealId:{}", dishId, setmealId);
        shoppingCart = shoppingCartService.updateCartApi(dishId, setmealId);
        return R.success(shoppingCart);
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clearCartApi() {
        shoppingCartService.clearCartApi();
        return R.success("清空成功！");
    }
}
